package com.project.running.Service.Impl;

import com.project.running.Exception.CustomerException;
import com.project.running.Service.LoginService;

import com.project.running.bean.Graderecord;
import com.project.running.bean.Memberinfo;
import com.project.running.bean.Pointaction;
import com.project.running.bean.Pointrecord;
import com.project.running.dao.GraderecordMapper;
import com.project.running.dao.MemberinfoMapper;
import com.project.running.dao.PointactionMapper;
import com.project.running.dao.PointrecordMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/6-10-06-14:08
 * @Description：com.project.running.Service.Impl
 */
@Service
public class LoginServiceImpl implements LoginService {


    @Override
    public Memberinfo login(String username, String password, String verifyCode, String authCode) {

        if(authCode == null || "".equals(authCode.trim())){
            throw new CustomerException("请输入验证码！") ;
        }else {
            if(verifyCode.length() != authCode.length()) {
                throw new CustomerException("验证码输入错误！") ;
            }
            for (int i = 0; i < verifyCode.length(); i++) {
                if(verifyCode.charAt(i) != authCode.charAt(i)){
                    throw new CustomerException("验证码输入错误！") ;
                }
            }
        }
        return login(username,password);
    }

    @Autowired
    MemberinfoMapper mapper;
    @Autowired
    PointactionMapper ptMapper;
    @Autowired
    PointrecordMapper pcMapper ;
    @Autowired
    GraderecordMapper gcMapper;

    @Override
    public Memberinfo login(String username, String password) {
//        SqlSession session = SqlSessionUtil.openSession();
//        MemberinfoMapper mapper = session.getMapper(MemberinfoMapper.class);
//        PointactionMapper ptMapper = session.getMapper(PointactionMapper.class);
//        PointrecordMapper pcMapper = session.getMapper(PointrecordMapper.class);
//        GraderecordMapper gcMapper = session.getMapper(GraderecordMapper.class);
        Memberinfo member = mapper.findByName(username);        //根据用户名查找用户
        if(member == null){                          //未查找到用户
            throw new CustomerException("用户名不存在！") ;
        }else if(!member.getPassword().equals(password)){      //密码输入错误
            throw new CustomerException("用户密码输入错误！") ;
        }else if(member.getStatus() == 1) {              //用户为注销状态，
            throw new CustomerException("该用户已注销！") ;
        }
//        }else if(member.getIsonline() == 1) {             //用户已在其他地方登入
//            return "该用户在其他设备已登录";
//        }
        member.setIsonline(1L);                      //更新用户在线状态
        Date latestdate = member.getLatestdate();        //获取上次登录时间
        Date newDate = new Date(System.currentTimeMillis());
        System.out.println(member);
        if(latestdate == null ||latestdate.getTime()-System.currentTimeMillis() >= 1000*60*60*24){     //若是当天第一次登录则添加积分
            //更新积分
            Pointaction login = ptMapper.selectByName("LOGIN");   //获取登录项可获得的积分
            member.setPoint(member.getPoint() + login.getPoint());     //更新用户积分

            Graderecord graderecord = gcMapper.selectByPoint(member.getPoint());          //获取当前积分的积分等级
            member.setGradeid(graderecord.getId());                //更新积分等级

            Pointrecord pointrecord = new Pointrecord();
            pointrecord.setNickname(member.getNickname());
            pointrecord.setPointactionid(login.getId());
            pointrecord.setReceivedate(new Date(System.currentTimeMillis()));

            pcMapper.insert(pointrecord);              //插入积分流水表
        }

        //member.setLatestdate(new Date(System.currentTimeMillis()));

        mapper.updateByPrimaryKey(member);         //更新用户
//        session.commit();
//        session.close();

        return member;
    }
}
